import { Injectable } from '@angular/core';
@Injectable()
export class ScriptLoaderService {

    private scripts: any = {};

    load(...scripts: string[]) {
        this.scripts = scripts;
        let promises: any[] = [];
        scripts.forEach((script) => promises.push(this.loadScript(script)));
        return Promise.all(promises);
    }

    loadScript(name: string) {
        return new Promise((resolve, reject) => {
            let script = (document.createElement('script') as any);
            script.type = 'text/javascript';
            script.src = name;

            if (script.readyState) {  //IE
                script.onreadystatechange = () => {
                    if (script.readyState === 'loaded' || script.readyState === 'complete') {
                        script.onreadystatechange = null;
                        resolve({script: name, loaded: true, status: 'Loaded'});
                    }
                };
            } else {  //Others
                script.onload = () => {
                    resolve({script: name, loaded: true, status: 'Loaded'});
                };
            }

            script.onerror = (error: any) => resolve({script: name, loaded: false, status: 'Loaded'});
            document.getElementsByTagName('head')[0].appendChild(script);
        });
    }

    
    /**
     * 先检查是否已存在，若没有再加载
     * @param name 资源链接
     */
     TryloadScript(name: string, action:{():void}): void{
        let scripts = document.getElementsByTagName('script');
        let t: boolean = false;
        for (let i = 0; i < scripts.length; i++) {
          let el = scripts[i];
          let href = el.getAttribute('src');
          if (href && href.indexOf(name) >= 0) {
            t = true;
          }
        }
        if (!t) {
          this.loadScript(name).then(()=>{
            action();
          });
        }else{
            action();
        }
    }

}
